package com.adcc.loadsheet.billing.controller;

import com.adcc.loadsheet.billing.configuration.MysqlConfig;
import com.adcc.loadsheet.billing.configuration.OracleConfig;
import com.adcc.loadsheet.billing.mapper.mysql.MysqlMapper;
import com.adcc.loadsheet.billing.mapper.oracle.OracleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by AN on 2020-04-01.
 */
@Controller
public class TestController {

    @Autowired
    private MysqlMapper mysqlMapper;

    @Autowired
    private OracleMapper oracleMapper;

    @Autowired
    private MysqlConfig mysqlConfig;

    @Autowired
    private OracleConfig oracleConfig;

    /*@RequestMapping(value = "/")
    public String test1(){
        return "test";
    }*/

    @RequestMapping(value = "/test")
    @ResponseBody
    public String test(){
        return "ok";
    }

    @RequestMapping(value = "/db")
    @ResponseBody
    public String db(){
        List<String> mysql=mysqlMapper.test();
        List<String> oralce=oracleMapper.test();
        String Threads_connected="-";
        String Threads_running="-";
        String max_connection="-";
        JdbcTemplate jdbcTemplate=new JdbcTemplate(mysqlConfig.getDataSource());
        JdbcTemplate jdbcTemplate2=new JdbcTemplate(oracleConfig.getDataSource());
        String sql = "show status like  'Threads%'";
        List<Map<String, Object>> tables = jdbcTemplate.queryForList(sql);
        for(Map<String,Object> one:tables){
            if(one.get("Variable_name").equals("Threads_connected")){
                Threads_connected=one.get("Value").toString();
            }

            if(one.get("Variable_name").equals("Threads_running")){
                Threads_running=one.get("Value").toString();
            }
        }

        sql = "show variables like '%max_connection%'";
        tables = jdbcTemplate.queryForList(sql);
        if(tables.size()==1){
            max_connection=tables.get(0).get("Value").toString();
        }
        return Threads_connected+"/"+Threads_running+"/"+max_connection;
    }
}
